﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _2011DBProject
{
    public partial class GamePlay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("onclick", "return confirm('게임을 시작하시겠습니까?');");
        }

        protected void btnChat_Click(object sender, EventArgs e)
        {
            enterChatText();
        }

        private void enterChatText()
        {
            try
            {
                DataSet1.CONVERSATIONDataTable conversation = new DataSet1.CONVERSATIONDataTable();
                DataSet1TableAdapters.CONVERSATIONTableAdapter taConversation = new _2011DBProject.DataSet1TableAdapters.CONVERSATIONTableAdapter();
                taConversation.FillBySeverNoChannelNo(conversation, Settings.decServer, Settings.decCh);
                DataSet1.CONVERSATIONRow row = conversation.NewCONVERSATIONRow();
                row.CHANNEL_GUILD_CAT = "";
                row.CHANNEL_NO = Settings.decCh;
                row.CHARACTER_NAME = Settings.strCharacterName;
                row.CONTENT = txtChat.Text.Trim();
                row.CONVERSATION_DATETIME = DateTime.Now;
                row.CONVERSATION_NO = getNextConversationNo();
                row.SERVER_NO = Settings.decServer;
                row.GAMEROOM_NO = Settings.decGameroom_No;
                conversation.AddCONVERSATIONRow(row);
                taConversation.Update(conversation);
                sqldsConversation.DataBind();
                txtChat.Text = "";
                gvConversationCh.DataBind();
            }
            catch (Exception)
            {
                sqldsConversation.DataBind();
                txtChat.Text = "";
                throw;
            }
        }

        private decimal getNextConversationNo()
        {
            decimal r = 1;
            try
            {
                System.Data.OracleClient.OracleConnection cnn = new System.Data.OracleClient.OracleConnection(Settings.strConnectionString);
                System.Data.OracleClient.OracleCommand command = new System.Data.OracleClient.OracleCommand("select count(*) from CONVERSATION where SERVER_NO = '" + Settings.decServer + "' and SERVER_NO = " + Settings.decServer + " and CHANNEL_NO = " + Settings.decCh, cnn);
                cnn.Open();
                r = Convert.ToDecimal(command.ExecuteScalar());
                cnn.Close();
                return r;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void exitGameroom()
        {
            try
            {
                DataSet1.GAMEROOM_ENTRANCE_STATUSDataTable chEntrance = new DataSet1.GAMEROOM_ENTRANCE_STATUSDataTable();
                DataSet1TableAdapters.GAMEROOM_ENTRANCE_STATUSTableAdapter taChEntrance = new _2011DBProject.DataSet1TableAdapters.GAMEROOM_ENTRANCE_STATUSTableAdapter();
                if (taChEntrance.FillByServerNoChannelNoGameroomNoCharacterName(chEntrance, Settings.decServer, Settings.decCh, Settings.decGameroom_No, Settings.strCharacterName) > 0)
                {
                    DataSet1.GAMEROOM_ENTRANCE_STATUSRow row = chEntrance[0];
                    row.Delete();
                    taChEntrance.Update(chEntrance);
                }

            }
            catch (Exception)
            {

            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblMember.Text = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btnMessage_Click(object sender, EventArgs e)
        {
            try
            {
                if (lblMember.Text != "선택된 멤버가 없습니다.")
                {
                    DataSet1TableAdapters.MESSAGETableAdapter taMsg = new DataSet1TableAdapters.MESSAGETableAdapter();
                    taMsg.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.MESSAGEDataTable tbMsg = taMsg.GetDataByServerNoCharacterName(Settings.decServer, lblMember.Text);
                    DataSet1.MESSAGERow row = tbMsg.NewMESSAGERow();
                    row.CHARACTER_NAME = lblMember.Text;
                    row.CONTENT = txtMessage.Text;
                    row.MESSAGE_DATETIME = System.DateTime.Now;
                    row.SERVER_NO = Settings.decServer;
                    tbMsg.AddMESSAGERow(row);
                    taMsg.Update(tbMsg);
                    sqldsGameroomEntranceStatus.DataBind();
                    GridView1.DataBind();
                    sqldsMessage.DataBind();
                    gvMessage.DataBind();
                    txtMessage.Text = "";
                    lblMember.Text = "선택된 멤버가 없습니다.";
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void btnGoLogout_onclick(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        protected void btnGoServer_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Server.aspx");
        }
        protected void btnGoCharacter_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Character.aspx");
        }
        protected void btnGoChannel_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Channel.aspx");
        }
        protected void btnGoMain_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Gameroom.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("GameEnd.aspx");
        }
    }
}
